#include "StdAfx.h"
#include <stdlib.h>
#include "patternNode.h"

patternNode::patternNode()
{
	sameColor=NULL;
	diffColor=NULL;
	empty=NULL;
	victory=-1;
	visits=-1;
	line=-1;
	evalValue=-1;
	board=NULL;
}

patternNode::patternNode(double eval)
{
	sameColor=NULL;
	diffColor=NULL;
	empty=NULL;
	victory=-1;
	visits=-1;
	line=-1;
	evalValue=eval;
	board=NULL;
}

patternNode::~patternNode()
{
	sameColor=NULL;
	diffColor=NULL;
	board=NULL;
}

void patternNode::setSameColor(patternNode newNode)
{
	*sameColor=newNode;
}

void patternNode::setDiffColor(patternNode newNode)
{
	*diffColor=newNode;
}

void patternNode::setEmpty(patternNode newNode)
{
	*empty=newNode;
}

void patternNode::setSibling(patternNode newNode)
{
	*sibling=newNode;
}

patternNode* patternNode::getSibling()
{
	return sibling;
}

void patternNode::setLine(int lineNumber)
{
	line=lineNumber;
}

int patternNode::getLine()
{
	return line;
}

double patternNode::getEvalValue()
{
	return evalValue;
}

void patternNode::setEvalValue(double newValue)
{
	evalValue=newValue;
}

patternNode* patternNode::getSameColor()
{
	return sameColor;
}

patternNode* patternNode::getDiffColor()
{
	return diffColor;
}

patternNode* patternNode::getEmpty()
{
	return empty;
}

void patternNode::setBoard(Board * newBoard)
{
	board=newBoard;
}

Board* patternNode::getBoard()
{
	return board;
}

void patternNode::chainTrees(int linesNumber)
{
	patternNode* current;
	current=this;
	patternNode* memSibling;
	current->setLine(1);
	current->developTree(linesNumber, memSibling);

	for(int i=2; i<=linesNumber;i++)
	{
		patternNode* memSibling;
		patternNode* newNode= new patternNode;
		current->setSibling(*newNode);
		current=current->getSibling();
		current->setLine(i);
		current->developTree(linesNumber, memSibling);
	}
}

void patternNode::developTree(int devLimit, patternNode* memSibling)
{
		patternNode *newNode = new patternNode();
		this->setSameColor(*newNode);
		if(devLimit<8)
			this->getSameColor()->developTree(devLimit+1, memSibling);
		else if (devLimit=8)
		{
			ifstream myfile;
			myfile.open ("valuesTree.txt");
			if(!myfile) 
			{ // file couldn't be opened
				cout << "Error: file could not be opened" << endl;
				exit(1);
			}
			int buffer;
			myfile>>buffer;
			double number=double(buffer);
			number=number/1000000;
			this->setEvalValue(number);
			myfile.close();
			if(memSibling!=NULL)
			{
				memSibling->setSibling(*this);
				memSibling=this;
			}
			else
				memSibling=this;
		}

		newNode = new patternNode();
		this->setDiffColor(*newNode);
		if(devLimit<8)
			this->getDiffColor()->developTree(devLimit+1, memSibling);
		else if (devLimit=8)
		{
			ifstream myfile;
			myfile.open ("valuesTree.txt");
			if(!myfile) 
			{ // file couldn't be opened
				cout << "Error: file could not be opened" << endl;
				exit(1);
			}
			int buffer;
			myfile>>buffer;
			double number=double(buffer);
			number=number/1000000;
			this->setEvalValue(number);
			myfile.close();
			if(memSibling!=NULL)
			{
				memSibling->setSibling(*this);
				memSibling=this;
			}
			else
				memSibling=this;
		}

		newNode = new patternNode();
		this->setEmpty(*newNode);
		if(devLimit<8)
			this->getEmpty()->developTree(devLimit+1, memSibling);
		else if (devLimit=8)
		{
			ifstream myfile;
			myfile.open ("valuesTree.txt");
			if(!myfile) 
			{ // file couldn't be opened
				cout << "Error: file could not be opened" << endl;
				exit(1);
			}
			int buffer;
			myfile>>buffer;
			double number=double(buffer);
			number=number/1000000;
			this->setEvalValue(number);
			myfile.close();
			if(memSibling!=NULL)
			{
				memSibling->setSibling(*this);
				memSibling=this;
			}
			else
				memSibling=this;
		}
}

void patternNode::saveTreeValues()
{
	ofstream myfile;
	//char temp[40];
	//_itoa_s (lineNumber,temp,10);
	myfile.open ("valuesTree.txt");
	patternNode *tempNode;
	tempNode=this;
	while(tempNode->getSameColor() !=NULL)
		tempNode=tempNode->getSameColor();
	
	while(tempNode->getEmpty()!=NULL)
	{
		double eval=tempNode->getEvalValue();
		int buff;
		eval=1000000*eval;
		buff=int(eval);
		myfile << buff;
		myfile << " ";
		tempNode=tempNode->getEmpty();
	}
	myfile.close();
}

void patternNode::chooseBranch(int posx, int posy, patternNode * result, int vect[], int length)
{
	for(int i=0; i<length; i=i+2)
	{
		if (result->getEvalValue()!=NULL)
		{
			if(board->isEmpty(posx+vect[i],posy+vect[i+1]))
				result=result->getEmpty();
			else 
			{
				if (board->isBlack(posx+vect[i],posy+vect[i+1]) && color)
					result=result->getSameColor();
				else
					result=result->getDiffColor();
			}
		}
	}
}

double patternNode::findEvalFromTree(patternNode * rootTrees, int posx, int posy, bool color)
{
	int lineNumber, lineNumber2;
	patternNode* result;
	bool cadranX=false;
	bool cadranY=false;
	
	if((this->getSize()+1)/2<=posx)
	{
		lineNumber=this->getSize()-posx;
		cadranX=true;
	}
	else
		lineNumber=posx;
	
	if((this->getSize()+1)/2<=posy)
	{
		lineNumber2=this->getSize()-posy;
		cadranY=true;
	}
	else
		lineNumber2=posy;

	if(lineNumber>lineNumber2)
		lineNumber=lineNumber2;

	result=rootTrees;
	while(result->getLine()!=lineNumber)
		result=result->getSibling();

	if(cadranX==false && cadranY==false)
	{
		if(posx-1>=0)
		{
			if(posy-1>=0)
			{
				int vect[18]={-1,-1,-1,0,-1,+1};
				//chooseBranch
			}
			else
			{
				
			}
		}
	}
	else if (cadranX==true && cadranY==false)
	{
		
	}
	else if (cadranX==false && cadranY==true)
	{
		
	}
	else if (cadranX==true && cadranY==true)
	{
		
	}
	
	return result->getEvalValue();
}